import { BaseWindow } from "../../../../component/window/BaseWindow";
import { ILayer } from "../../../../mvc/interfaces/ILayer";
import { Language } from "../../../../../extend/language/Language";
import { Dispatcher } from "../../../../mvc/core/Dispatcher";
import { EventName } from "../../../mvc/EventName";
import { NetDispatcher } from "../../../../mvc/core/NetDispatcher";
import { ServerCommand } from "../../../mvc/ServerCommand";
import { DataEvent } from "../../../events/DataEvent";
import { MsgManager } from "../../../manager/MsgManager";
import { Cache } from "../../../cache/Cache";
import { PetExploreConfig } from "../../../resource/PetExploreConfig";
import { PetExploreUtil } from "./info/PetExploreUtil";
import { PetExploreAreaConfig } from "../../../resource/PetExploreAreaConfig";
import { PetExploreAutoAlertWindow } from "./PetExploreAutoAlertWindow";
import { SPetUpdate } from "../../../../../Message/Game/SPetUpdate";
import { SPetExploreInfo } from "../../../../../Message/Public/SPetExploreInfo";
import { EffectManager } from "../../../manager/EffectManager";
import { FilterConst } from "../../../../component/gconst/FilterConst";
import { SPetExploreCards } from "../../../../../Message/Public/SPetExploreCards";
import { PetExploreMapPanel } from "./PetExploreMapPanel";
import { ObjCreate } from "../../common/ObjCreate";
import { GTextFormat } from "../../../../common/GTextFormat";
import { FontUtil } from "../../../../common/font/FontUtil";
import { PetExploreRecordsPanel } from "./PetExploreRecordsPanel";
import { ResFileConst } from "../../../resource/ResFileConst";
import { PetExploreSelectPanel } from "./PetExploreSelectPanel";
import { PetExploreCardsPanel } from "./PetExploreCardsPanel";
import { PetExploreAutoResultWindow } from "./PetExploreAutoResultWindow";
import { GButton } from "../../../../../com/mui/controls/GButton";
import { GLoadedButton } from "../../../../../com/mui/controls/GLoadedButton";
import { GCheckBox } from "../../../../../com/mui/controls/GCheckBox";
import { _multiple } from "../../../../../this/_multiple";
type int = number;
//class PetExploreWindow
    
    export  class PetExploreWindow extends BaseWindow
    {
        constructor(arg1: ILayer=null)
        {
            super(arg1);
            this.setSize(685, 490);
            this.titleHeight = 28;
            this.title = Language.getString(54529);
            Dispatcher.addEventListener(EventName.PetBagUpdate, this.onPetBagUpdateHandler);
            NetDispatcher.addCmdListener(ServerCommand.PetExploreInfoCommand, this.onPetExploreInfoCommandHandler);
            NetDispatcher.addCmdListener(ServerCommand.PetExplorePackCommand, this.onPetExplorePackItemUpdateHandler);
            NetDispatcher.addCmdListener(ServerCommand.PetExplorePackItemUpdate, this.onPetExplorePackItemUpdateHandler);
            return;
        }

        /* internal  */onAutoResultWindowHide(): void
        {
            this.showMask(false);
            return;
        }

        public updateAutoResult(arg1: Array<any>): void
        {
            if (this._autoResultWindow && !this._autoResultWindow.isHide) 
            {
                this._autoResultWindow.updateByResultArray(arg1);
                this.updateExploreNum();
            }
            return;
        }

        public showMask(arg1: Boolean): void
        {
            if (arg1) 
            {
                this._maskSprite.mouseEnabled = true;
                this._maskSprite.graphics.clear();
                this._maskSprite.graphics.lineStyle(0, 0, 0);
                this._maskSprite.graphics.beginFill(0, 0.3);
                this._maskSprite.graphics.drawRect(0, 0, 650, 440);
            }
            else 
            {
                this._maskSprite.mouseEnabled = false;
                this._maskSprite.graphics.clear();
            }
            return;
        }

        /* internal  */pageTurningHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            if (arg1.target.name != "leftBtn") 
            {
                if (arg1.target.name == "rightBtn") 
                {
                    this._mapPanel.updataByPageTurning = 1;
                    this._leftBtn.visible = true;
                    this._rightBtn.visible = false;
                }
            }
            else 
            {
                this._mapPanel.updataByPageTurning = 0;
                this._rightBtn.visible = true;
                this._leftBtn.visible = false;
            }
            return;
        }

        public closeAutoResultWindow(): void
        {
            if (this._autoResultWindow) 
            {
                this._autoResultWindow.hide();
            }
            return;
        }

        /* internal  */onGetRewardsBtnClickHandler(arg1: Object /* flash.events.Event */): void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.PetExploreGetRewardWindowOpen));
            return;
        }

        /* internal  */onPackBtnClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.PetExplorePackWindowOpen));
            return;
        }

        /* internal  */onStartBtnClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            var loc4=null;
            var loc5=0;
            var loc6=0;
            var loc7=false;
            var loc8=0;
            var loc9=null;
            var loc10=null;
            var loc1=this._autoCheckBox.selected;
            var loc2=this._selectPanel.getBattlePetUids();
            if (loc2.length == 0) 
            {
                MsgManager.showRollTipsMsg(Language.getString(54512));
                this._selectPanel.showPanel(true);
                return;
            }
            if (!Cache.instance.petExplore.isCanExploreToday()) 
            {
                MsgManager.showRollTipsMsg(Language.getString(54507));
                return;
            }
            var loc3;
            if (loc3 = Cache.instance.petExplore.petExploreInfo) 
            {
                loc5 = loc3.currentLevel != 0 ? loc3.currentLevel : 1;
                loc4 = PetExploreConfig.instance.getExploreConfigByLevel(loc5);
            }
            if (loc4) 
            {
                if (Cache.instance.petExplore.petExploreInfo.isInExplore) 
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.PetExploreWatch, true));
                }
                else 
                {
                    if (loc1) 
                    {
                        if (!PetExploreUtil.isAutoExploreMoneyEnough()) 
                        {
                            Dispatcher.dispatchEvent(new DataEvent(EventName.ShopMallAutoBuyCoin, Cache.instance.petExplore.getAutoExploreTotalCost()));
                            return;
                        }
                        if (!Cache.instance.petExplore.isActive) 
                        {
                            this._multiple = 1;
                        }
                    }
                    else 
                    {
                        this._multiple = 1;
                        if (Cache.instance.petExplore.isCanSkipExplore()) 
                        {
                            Dispatcher.dispatchEvent(new DataEvent(EventName.PetExploreSkipWindowOpen));
                            return;
                        }
                        if (Cache.instance.petExplore.isAgainExplore()) 
                        {
                            Dispatcher.dispatchEvent(new DataEvent(EventName.PetExploreAgainWindowOpen));
                            return;
                        }
                    }
                    Cache.instance.petExplore.selectedConfigInfo = loc4;
                    loc6 = loc4.level;
                    loc7 = false;
                    Dispatcher.dispatchEvent(new DataEvent(EventName.PetExploreStart, {"startLevel": loc6, "isAutoExplore": loc1, "isDrawRewards": loc7, "callBack": this.onStartExploreReponse, "multiple": _multiple}));
                }
            }
            else 
            {
                loc8 = Cache.instance.petExplore.petExploreInfo.currentLevel;
                if ((loc9 = PetExploreConfig.instance.getExploreConfigByLevel(loc8)) && loc9.area > Cache.instance.petExplore.getExploreAreaIndex()) 
                {
                    loc10 = PetExploreAreaConfig.instance.getAreaConfigByArea(loc9.area);
                    MsgManager.showRollTipsMsg(Language.getStringByParam(54527, loc10.name));
                }
            }
            return;
        }

        /* internal  */onStartExploreReponse(): void
        {
            var loc1=Cache.instance.petExplore.isAutoExplore();
            if (!loc1) 
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.PetExploreBattleWindowOpen));
            }
            Cache.instance.petExplore.initJoinPet();
            return;
        }

        /* internal  */onAutoCheckBoxChangeHandler(arg1: Object /* flash.events.Event */): void
        {
            var loc1=this._autoCheckBox.selected;
            if (loc1) 
            {
                if (!this._autoAlertWindow) 
                {
                    this._autoAlertWindow = new PetExploreAutoAlertWindow(this.onAutoCheckBoxSelHandler);
                }
                this._autoAlertWindow.show();
            }
            return;
        }

        /* internal  */onAutoCheckBoxSelHandler(arg1: Boolean, arg2: int): void
        {
            this._autoCheckBox.selected = arg1;
            this._multiple = arg2;
            if (arg1) 
            {
                this.onStartBtnClickHandler(null);
                this._autoCheckBox.selected = false;
            }
            return;
        }

        /* internal  */onPetBagUpdateHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as SPetUpdate;
            this._selectPanel.updatePackPet(loc1.updateType == 4);
            return;
        }

        /* internal  */onRecordsBtnClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            this._recordsPanel.visible = !this._recordsPanel.visible;
            return;
        }

        public resetGetRewardsBtn(): void
        {
            this._getRewardsBtn.visible = Cache.instance.petExplore.isCanGetReward();
            return;
        }

        /* internal  */updateTipTxt(): void
        {
            if (Cache.instance.petExplore.isCanExploreToday()) 
            {
                this._tipTxt.htmlText = Language.getString(54537);
            }
            else 
            {
                this._tipTxt.htmlText = Language.getString(54536);
            }
            return;
        }

        /* internal  */onPetExploreInfoCommandHandler(arg1: SPetExploreInfo): void
        {
            var loc1=null;
            var loc2=0;
            this.resetExploreButton();
            this._selectPanel.resetSaveButton();
            if (arg1) 
            {
                loc2 = arg1.currentLevel != 0 ? arg1.currentLevel : 1;
                loc1 = PetExploreConfig.instance.getExploreConfigByLevel(loc2);
            }
            if (arg1.autoExplore) 
            {
                this.showAutoResultWindow();
            }
            if (loc1) 
            {
                this._autoCheckBox.enabled = !arg1.isInExplore;
            }
            else 
            {
                this._autoCheckBox.enabled = false;
            }
            this._autoCheckBox.selected = arg1.autoExplore;
            return;
        }

        /* internal  */alertPackButton(): void
        {
            var loc1=Cache.instance.pack.petExplorePackCache;
            var loc2=loc1.getItemsByJudge().length;
            if (loc2 > 0) 
            {
                EffectManager.glowFilterReg(this._packBtn, [FilterConst.hintIconFilter], 0.3, 6, 3, 0);
            }
            else 
            {
                EffectManager.glowFilterUnReg(this._packBtn);
            }
            return;
        }

        public updateCardsPanel(arg1: SPetExploreCards): void
        {
            this._cardsPanel.updateCards(arg1.cards, true);
            return;
        }

        /* internal  */onPetExplorePackItemUpdateHandler(arg1: Object): void
        {
            this.alertPackButton();
            return;
        }

        public /* override */ show(arg1: int=0, arg2: int=0): void
        {
            super.show(arg1, arg2);
            this.updateExploreNum();
            this._selectPanel.updatePackPet(false);
            this._selectPanel.resetSaveButton();
            this.resetGetRewardsBtn();
            this.resetExploreButton();
            this.updateTipTxt();
            this.alertPackButton();
            this.updateCardsPanel(Cache.instance.petExplore.skillCards);
            if (Cache.instance.petExplore.isAutoExplore()) 
            {
                this.showAutoResultWindow();
            }
            this.onPetExploreInfoCommandHandler(Cache.instance.petExplore.petExploreInfo);
            EffectManager.glowFilterReg(this._autoCheckBox, [FilterConst.uiBtnFilter], 1, 10, 0, 10);
            return;
        }

        protected /* override */ createChildren(): void
        {
            super.createChildren();
            this._mapPanel = new PetExploreMapPanel();
            this._mapPanel.x = 18;
            this._mapPanel.y = 40;
            this._mapPanel.enableArea(1, true);
            this.addChild(this._mapPanel);
            ObjCreate.createTextField(Language.getString(54530), 27, 50, 220, 20, this, new GTextFormat(FontUtil.songtiName, 12, 16711680));
            this._exploredTxt = ObjCreate.createTextField("", 115, 50, 220, 20, this, new GTextFormat(FontUtil.songtiName, 12, 16711680));
            ObjCreate.createTextField(Language.getString(54531), 27, 70, 220, 20, this, new GTextFormat(FontUtil.songtiName, 12, 16777215));
            this._unExploredTxt = ObjCreate.createTextField("", 115, 70, 220, 20, this, new GTextFormat(FontUtil.songtiName, 12, 16777215));
            this._mapNameTxt = ObjCreate.createTextField(Language.getString(54532), 0, 40, 685, 40, this, new GTextFormat(FontUtil.xingkaiName, 26, 15055415));
            this._mapNameTxt.mouseEnabled = false;
            this._mapNameTxt.autoSize = flash.text.TextFieldAutoSize.CENTER;
            this._tipTxt = ObjCreate.createTextField("", 0, 75, 685, 40, this, new GTextFormat(FontUtil.xingkaiName, 20, 16711680));
            this._tipTxt.mouseEnabled = false;
            this._tipTxt.autoSize = flash.text.TextFieldAutoSize.CENTER;
            this._getRewardsBtn = ObjCreate.createGButton(Language.getString(54495), 500, 50, 70, 24, this);
            this._getRewardsBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onGetRewardsBtnClickHandler);
            this._packBtn = ObjCreate.createGButton(Language.getString(54533), 590, 50, 70, 24, this);
            this._packBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onPackBtnClickHandler);
            this._recordsBtn = ObjCreate.createGButton(Language.getString(54534), 590, 80, 70, 24, this);
            this._recordsBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onRecordsBtnClickHandler);
            this._recordsPanel = new PetExploreRecordsPanel();
            this._recordsPanel.x = 685;
            this._recordsPanel.y = 85;
            this._recordsPanel.visible = false;
            this.addChild(this._recordsPanel);
            this._startBtn = ObjCreate.createGLoadedButton(ResFileConst.StartExplore, 285, 437, 121, 40, this);
            this._startBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onStartBtnClickHandler);
            this._autoCheckBox = ObjCreate.createCheckBox(Language.getString(54636), 415, 445, 150, 28, this);
            this._autoCheckBox.addEventListener(flash.events.Event.CHANGE, this.onAutoCheckBoxChangeHandler);
            this._autoCheckBox.textField.filters = [FilterConst.vipGlowFilter];
            this._selWinMask = new Object /* flash.display.Sprite */();
            this._selWinMask.graphics.beginFill(16777215, 0);
            this._selWinMask.graphics.drawRect(17, 100, 365, 265);
            this._selWinMask.graphics.endFill();
            this.addChild(this._selWinMask);
            this._leftBtn = ObjCreate.createGLoadedButton(ResFileConst.ArrowBtn, 85, 300, 63, 39, this);
            this._leftBtn.name = "leftBtn";
            this._leftBtn.rotation = 180;
            this._leftBtn.visible = false;
            this._leftBtn.addEventListener(flash.events.MouseEvent.CLICK, this.pageTurningHandler);
            this._rightBtn = ObjCreate.createGLoadedButton(ResFileConst.ArrowBtn, 600, 260, 63, 39, this);
            this._rightBtn.name = "rightBtn";
            this._rightBtn.addEventListener(flash.events.MouseEvent.CLICK, this.pageTurningHandler);
            this._selectPanel = new PetExploreSelectPanel();
            this._selectPanel.x = -310;
            this._selectPanel.y = 100;
            this._selectPanel.mask = this._selWinMask;
            this.addChild(this._selectPanel);
            this._cardsPanel = new PetExploreCardsPanel();
            this._cardsPanel.x = 640;
            this._cardsPanel.y = 360;
            this.addChild(this._cardsPanel);
            this.updateExploreNum();
            this._maskSprite = new Object /* flash.display.Sprite */();
            this._maskSprite.x = 18;
            this._maskSprite.y = 40;
            this._maskSprite.mouseEnabled = false;
            this.addChild(this._maskSprite);
            return;
        }

        public showSelectPanel(arg1: Boolean): void
        {
            this._selectPanel.showPanel(arg1);
            return;
        }

        /* internal  */resetExploreButton(): void
        {
            if (Cache.instance.petExplore.petExploreInfo.isInExplore) 
            {
                this._startBtn.styleName = ResFileConst.WatchExplore;
            }
            else 
            {
                this._startBtn.styleName = ResFileConst.StartExplore;
            }
            return;
        }

        public getToBattlePetUids(): Array<any>
        {
            return this._selectPanel.getBattlePetUids();
        }

        /* internal  */updateExploreNum(): void
        {
            this._exploredTxt.text = Cache.instance.petExplore.getExploredNum().toString();
            this._unExploredTxt.text = Cache.instance.petExplore.getUnExploreNum().toString();
            return;
        }

        public showAutoResultWindow(): void
        {
            if (this._autoResultWindow && !this._autoResultWindow.isHide) 
            {
                return;
            }
            if (!this._autoResultWindow) 
            {
                this._autoResultWindow = new PetExploreAutoResultWindow(this, this.onAutoResultWindowHide);
            }
            this.showMask(true);
            this._autoResultWindow.updateByResultArray(Cache.instance.petExplore.autoResultArray);
            this._autoResultWindow.show();
            return;
        }

        /* internal */ /* var */_mapPanel: PetExploreMapPanel;

        /* internal */ /* var */_exploredTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */_unExploredTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */_mapNameTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */_selWinMask: Object /* flash.display.Sprite */;

        /* internal */ /* var */_selectPanel: PetExploreSelectPanel;

        /* internal */ /* var */_getRewardsBtn: GButton;

        /* internal */ /* var */_packBtn: GButton;

        /* internal */ /* var */_startBtn: GLoadedButton;

        /* internal */ /* var */_autoCheckBox: GCheckBox;

        /* internal */ /* var */_autoAlertWindow: PetExploreAutoAlertWindow;

        /* internal */ /* var */_autoResultWindow: PetExploreAutoResultWindow;

        /* internal */ /* var */_maskSprite: Object /* flash.display.Sprite */;

        /* internal */ /* var */_recordsBtn: GButton;

        /* internal */ /* var */_recordsPanel: PetExploreRecordsPanel;

        /* internal */ /* var */_cardsPanel: PetExploreCardsPanel;

        /* internal */ /* var */_rightBtn: GLoadedButton;

        /* internal */ /* var */_multiple: int;

        /* internal */ /* var */_tipTxt: Object /* flash.text.TextField */;

        /* internal */ /* var */_leftBtn: GLoadedButton;
    }
